/*
 *  Class name: DataViewerController.java
 *  Version: 1.0
 *  Date: 19.6.2014
 */
package cz.morosystems.personwebmanager.presentationlayer.controllers;

import cz.morosystems.personwebmanager.businesslayer.sevices.UserService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Michal Ďuriš
 */
@Controller
public class DataViewerController {

    @Autowired
    private UserService userService;

    public DataViewerController() {
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    @RequestMapping("/index")
    public String getUsers(Model model) {
        
        model.addAttribute("users", userService.getAllUsers());
        
        return "index";
    }

    @RequestMapping("/viewdata")
    public String getTestData(Model model) {

        model.addAttribute("testString", getTestString());
        model.addAttribute("testDate", getTestDate());
        model.addAttribute("testList", getTestList());
        model.addAttribute("testMap", getTestMap());

        return "test";
    }

    @RequestMapping(value = "/viewdata", params = "userID")
    public String getUser(@RequestParam("userID") Long userID, Model model) {

        model.addAttribute("user", userService.getUserByID(userID));
        
        return "userView";
    }

    private String getTestString() {

        return "test string";
    }

    private Date getTestDate() {

        return new Date();
    }

    private List<String> getTestList() {

        List<String> testList = new ArrayList<>();

        testList.add("mozno");
        testList.add("asi");
        testList.add("urcite");

        return testList;
    }

    private Map<String, String> getTestMap() {

        Map<String, String> testMap = new HashMap<>();

        testMap.put("key", "value");
        testMap.put("kluc", "hodnota");

        return testMap;
    }

}
